package org.lightrpc.format.json;

import org.lightrpc.Response;
import org.lightrpc.format.DataSerializer;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class GsonDataSerializer implements DataSerializer {

	private final Gson gson;

	public GsonDataSerializer() {
		gson = new GsonBuilder().serializeNulls().create();
	}

	@Override
	public <T> T deserialize(Response response, Class<T> clazz) {
		return gson.fromJson(response.getData(), clazz);
	}

	@Override
	public Response serialize(Response response, Object object) {
		response.setData(gson.toJson(object));
		return response;
	}
}